﻿using System.IO;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace Lean.NantTasks.AnsiToUtf
{
    /// <summary>
    /// Converting all files in a given directory with a given extension from ansi to utf
    /// Primarily used to convert sql-scripts to utf for special character consistency 
    /// </summary>
    [TaskName("ansitoutf")]
    public class AnsiToUtfEncodingTask : Task
    {

        [TaskAttribute("dir"), StringValidator(AllowEmpty = false)]
        public DirectoryInfo Dir { get; set; }

        [TaskAttribute("extension"), StringValidator(AllowEmpty = true)]
        public string Extension { get; set; }


        private void Log(string message)
        {
            Project.Log(Level.Info, message);
        }


        protected override void ExecuteTask()
        {
            try
            {
                Log(string.Format("Converting all Files in Folder \"{0}\"", Dir.FullName));

                var cs = new ConversionService();
                cs.Convert(Dir, Extension);
            }
            catch
            {
                if (FailOnError)
                    throw;
            }
        }


    }
}
